package com.lxlg.lxlg_order.app.controller;


import com.lxlg.core.utils.Result;
import com.lxlg.lxlg_order.client.SeckillOrderClient;
import com.lxlg.lxlg_order.client.param.CancelSeckillOrderForm;
import com.lxlg.lxlg_order.client.param.PaySeckillOrderForm;
import com.lxlg.lxlg_order.client.param.SeckillOrderForm;
import com.lxlg.lxlg_order.client.result.SeckillOrderInfo;
import com.lxlg.lxlg_order.common.WebUtils;
import com.lxlg.lxlg_order.service.service.SeckillOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/infra/membership-levels")
public class SeckillOrderController implements SeckillOrderClient {
    @Resource
    private SeckillOrderService seckillOrderService;

    @PostMapping("newSeckillOrder")
    @ApiOperation("新增秒杀订单")
    public Result<SeckillOrderInfo> newSeckillOrder(@RequestBody @Valid SeckillOrderForm seckillOrderForm){
        return seckillOrderService.addSeckillOrder(seckillOrderForm);
    }
    @ApiOperation("取消订单")
    @PostMapping("cancelSeckillOrder")
    public Result<String> cancelSeckillOrder(@RequestBody @Valid CancelSeckillOrderForm cancelSeckillOrderForm){
        return seckillOrderService.cancleSeckillOrder(cancelSeckillOrderForm.getSeckillOrderCode());
    }

    @ApiOperation("支付订单")
    @PostMapping("paySeckillOrder")
    public void paySeckillOrder(@RequestBody @Valid PaySeckillOrderForm paySeckillOrderForm)  {
        HttpServletResponse resp = WebUtils.getResponse();
        try {
            seckillOrderService.doPaySeckillOrder(paySeckillOrderForm.getSeckillOrderCode(),resp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @PostMapping("paySeckillOrder")
//    public String chekSeckillCode(){
//         return seckillOrderService.chekSeckillCode();
//
//    }
}

